home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Commun⁄Network / Telnet 2.5.src.ThinkC / source / vdevice.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-14  |  5.7 KB  |  213 lines  |  [TEXT/MPS ]

  1. #ifndef __ALLNU__
  2. #define __ALLNU__
  3. #include <Events.h>
  4. #include <Controls.h>
  5. #include <OSUtils.h>
  6. #include <Dialogs.h>
  7. #include <Memory.h>
  8. #include <Quickdraw.h>
  9. #include <Palette.h>
  10. #include <Windows.h>
  11. #include <Menus.h>
  12. #include <Fonts.h>
  13. #endif
  14.  
  15. #include "vdevice.h"
  16.  
  17. extern int HasColor;    /* BYU - crashes on Mac Portable without this */
  18.  
  19. /*************************************************************************/
  20. /* Virtual drawing device code.
  21. *
  22. *  
  23. *  InitVDevice()        fill in the fields of a vdevice and allocate the gdevice.
  24. *  SetVDevice()         set the device and port for off-screen drawing.
  25. *                       this only pushes the old values 1-deep, no stack.
  26. *  UnsetVDevice()       set the device and port back to what they were.
  27. *  TrashVDevice()       dispose and close all fields in the vdevice.
  28. *  ColorVDevice()       set the color palette for the 8-bit vdevice.
  29. */
  30. GDHandle v_savegd;        /* the saved gdevice */
  31. CGrafPtr v_saveport;    /* the saved port from SetPort */
  32.  
  33. /*************************************************************************/
  34. /* InitVDevice
  35. *  allocate an off-screen pixmap and off-screen gdevice which can be drawn
  36. *  in without the palette manager affecting the colors.  If we use MakeITable
  37. *  ourselves and don't install the gdevice into the gdevice list or make it
  38. *  active, then the palette manager ignores it, but QuickDraw works on it.
  39. *
  40. *  set vdev->bounds to the size you need before calling.
  41. *  allocate vdev->bp enough space to hold one byte per pixel of a rectangle that
  42. *  size.
  43. *
  44. *  returns 0 if ok,
  45. *  returns -1 or other negative on other errors.
  46. */
  47. int InitVDevice
  48.   (
  49.     VDevicePtr vdev
  50.   )
  51. {
  52.     GDHandle thegd;
  53.     PixMapHandle pm;
  54.     Rect tr;
  55.     CTabHandle ct;
  56.     int width;
  57.  
  58.     GetPort((GrafPtr *) &v_saveport);
  59.     v_savegd = GetGDevice();            /* get old values */
  60.     
  61.     tr = vdev->bounds;                    /* get size of device to create */
  62.     if (tr.right - tr.left < 1 ||
  63.         tr.bottom - tr.top < 1)
  64.         return(-1);                        /* check for simple mistake */
  65.         
  66.     width = tr.right - tr.left;
  67.     
  68.     if (!vdev->bp)
  69.         return(-2);                        /* another simple mistake */
  70. /*
  71. *  Allocate the off-screen PixMap for drawing a duplicate copy.  The off-screen
  72. *  gdevice, CPort and PixMap form a virtual drawing space with its own color map.
  73. *  For this program, we have chosen to make this virtual space an 8-bit drawing
  74. *  device.  When drawing, use SetVDevice and UnsetVDevice to turn on and off.
  75. */
  76.     thegd = vdev->vgd = NewGDevice( 0, -1 );
  77.     
  78.     pm = (PixMapHandle) NewHandle(sizeof(PixMap));
  79.  
  80.     if (width & 1)    {                    /* must be even */
  81.         --tr.right;
  82.         --width;
  83.     }
  84.     (*pm)->baseAddr = (Ptr) vdev->bp;    /* BYU LSC - get memory to use */
  85.     (*pm)->rowBytes = width | 0x8000;    /* setting high flag bit */
  86.     (*pm)->bounds = tr;
  87.     (*pm)->pixelSize = 8;                /* source is 8-bits */
  88.     (*pm)->pixelType = 0;                /* chunky */
  89.     (*pm)->cmpCount = 1;                /* chunky */
  90.     (*pm)->cmpSize = 8;                    /* chunky */
  91.     (*pm)->planeBytes = 0;                /* chunky */
  92.     (*pm)->pmVersion = 0;                /* chunky */
  93.     (*pm)->packSize = 0;                /* chunky */
  94.     (*pm)->packType = 0;                /* chunky */
  95.     ct = (CTabHandle)NewHandle(sizeof(ColorTable));
  96.     (*pm)->pmTable = ct;
  97.     (*ct)->ctSeed = GetCTSeed();
  98.     (*ct)->ctFlags = 0x8000;
  99.     (*ct)->ctSize = 1;                    /* 1-length color table (empty) */
  100.     
  101.     (*thegd)->gdResPref = 3;            /* inverse table size preferred */
  102.     (*thegd)->gdRect = tr;                /* device size boundary */
  103.  
  104.     (*thegd)->gdPMap = pm;                /* copy pixmap handle */
  105.     
  106.     SetDeviceAttribute(thegd,noDriver,true);
  107.     SetDeviceAttribute(thegd,gdDevType,true);
  108.     
  109.     SetGDevice(thegd);
  110.                         /* CPort inherits from the gdevice, including pixmap */
  111.     if (HasColor)                        /* BYU */
  112.         OpenCPort(&vdev->vport);        /* BYU - initialize the port struct */
  113.     SetPort((GrafPtr) &vdev->vport);
  114.     ClipRect(&tr);                        /* set clip region */
  115.  
  116. /*
  117. *  Erase the image region in the virtual device. 
  118. */
  119.     EraseRect(&tr);
  120.  
  121. /*
  122. *  Restore the environment.
  123. */
  124.     SetGDevice(v_savegd);
  125.     SetPort((GrafPtr) v_saveport);
  126.     
  127.     return(0);
  128. }
  129.  
  130. /*******************************************************************************/
  131. /* SetVDevice
  132. *  Set the gdevice and port to our off-screen space.
  133. *  Save the old values for unset.
  134. */
  135. SetVDevice(vdev)
  136.     VDevicePtr vdev;
  137. {
  138.  
  139.     GetPort((GrafPtr *) &v_saveport);
  140.     v_savegd = GetGDevice();
  141.     if (!vdev->vgd)
  142.         return(-1);
  143.     SetGDevice(vdev->vgd);
  144.     SetPort((GrafPtr) &vdev->vport);
  145.     
  146.     return(0);
  147. }
  148.  
  149. /*******************************************************************************/
  150. /* UnsetVDevice
  151. *  Set the vdevice back to the saved values.
  152. */
  153. UnsetVDevice()
  154. {
  155.     SetGDevice(v_savegd);
  156.     SetPort((GrafPtr) v_saveport);
  157. }
  158.  
  159. /*******************************************************************************/
  160. /* TrashVDevice
  161. *  Get rid of the devices that we created with InitVDevice.
  162. *
  163. *  Remember to free up the vdev->bp after the gdevice is gone.
  164. */
  165. void TrashVDevice
  166.   (
  167.     VDevicePtr vdev
  168.   )
  169. {
  170.     
  171.     (*((*(vdev->vgd))->gdPMap))->baseAddr = NULL;    /* drop old value, bp has a copy */
  172.         
  173.     if (HasColor)                    /* BYU */
  174.         CloseCPort(&vdev->vport);    /* BYU - lose the cport, disposes the pixmap */
  175.     (*(vdev->vgd))->gdPMap = NULL;    /* destroy second copy of the pixmaphandle */
  176.     
  177.     DisposGDevice(vdev->vgd);        /* disposes current gdevice and pixmap */
  178.     vdev->vgd = NULL;
  179.  
  180. }
  181.  
  182. /*******************************************************************************/
  183. /*  ColorVDevice
  184. *   input:  vdev and 
  185. *        palette handle.
  186. *   
  187. *   Use palette2ctab to install the color table into the off-screen gdevice and
  188. *   make the inverse color table for it.  Also install the palette into the current
  189. *   window.
  190. */
  191. void ColorVDevice
  192.   (
  193.     VDevicePtr vdev,
  194.     PaletteHandle pal
  195.   )
  196. {
  197.     CTabHandle ct;
  198.  
  199.     ct = (*(*(vdev->vgd))->gdPMap)->pmTable;        /* handle from vdevice */
  200.     if (!ct)
  201.         return;
  202.     
  203.     Palette2CTab( pal, ct );
  204.  
  205.     (*ct)->ctSeed = GetCTSeed();                    /* give it a unique seed */
  206.     (*ct)->ctFlags = 0x8000;
  207.     
  208.     MakeITable( ct, (*(vdev->vgd))->gdITable, 3 );    /* 3-bit inverse table  */
  209.  
  210. }
  211.  
  212.